//IMPLEMENTATION OF LISTS USING ARRAYS
#include<stdio.h>
//FINDS THE MAXIMUM ELEMENT IN THE LIST
void max(int n,int a[])
{
 int max_no,i;
 max_no=a[0];
 for(i=0;i<n;i++)
 {
  if(a[i]>max_no)
  {
   max_no=a[i];
  }
 }
 printf("maximum no:%d\n",max_no);
}
//FINDS THE MINIMUM ELEMENT IN THE LIST
void min(int n,int a[])
{
 int min_no,i;
 min_no=a[0];
 for(i=0;i<n;i++)
 {
  if(a[i]<min_no)
  {
   min_no=a[i];
  }
 }
 printf("minimum no:%d\n",min_no);
}
//REVERSES THE LIST
void reverse(int n,int a[])
{
 int i,j,k;
 for(i=0,j=n-1;i<n/2;i++,j--)
 {
  k=a[i];
  a[i]=a[j];
  a[j]=k;
 }
}
//FINDS THE INDEX VALUE OF AN ELEMENT
void index1(int n,int a[])
{
 int x,i,flag=0;
 printf("enter the no:\n");
 fflush(stdout);
 scanf(" %d",&x);
 for(i=0;i<n;i++)
 {
  if(a[i]==x)
  { printf("first occurence of %d is index %d \n",x,i);
    flag=1;
    break;
  }
 }
 if(flag==0)
 {printf("number doesn't exist \n");}
}
//COUNTS THE NUMBER OF OCCURENCES OF AN ELEMENT
void count(int n,int a[])
{
 int x,i,sum=0,flag=0;
 printf("enter no:\n");
 scanf("%d",&x);
 for(i=0;i<n;i++)
 {
  if(a[i]==x)
  {
   sum+=1;
   flag=1;
   continue;
  }}
 if(flag==1)
 {printf("total no of occurences of %d is %d\n",x,sum);}
 else
 {printf("number doesn't exist \n");}
}
//SORTS THE LIST
void sort(int n,int a[])
{
 int i,temp,j;
 for(i=0;i<n;i++)
 {for(j=i;j<n;j++)
  {if(a[j]<a[i])
   {temp=a[i];
    a[i]=a[j];
    a[j]=temp;}
  }
 }
}
//DELETES THE ELEMENT OF INDEX "i"
void pop(int n,int a[],int i)
{int k;
 for(k=i;k<n;k++)
 {
  a[k]=a[k+1];
  }
}
//DELETES THE ELEMENT
int delete(int n, int a[])
{int x,flag=0,i,k;
 printf("enter the element you want to delete:\n");
 scanf("%d",&x);
 for(i=0;i<n;i++)
 {if(a[i]==x)
  {for(k=i;k<n;k++)
   {
    a[k]=a[k+1];
   }
   flag=1;
  }}
  return flag;
}
//APPENDS AN ELEMENT AT THE END OF LIST
void append(int n,int a[])
{ int x;
  printf("enter the element you want to append:\n");
  scanf("%d",&x);
  *(a+n)=x;
}
//EXTENDS THE LIST BY ANOTHER LIST
void extend(int n,int a[],int p)
{ int b[p],i,k,j;
  for(i=0;i<p;i++)
  {
   printf("enter b[%d]:\n",i);
   scanf("%d",&b[i]); 
  }
  for(k=n,j=0;k<(n+p),j<p;k++,j++)
  {*(a+k)=b[j];}
} 
//INSERTS AN ELEMENT AT INDEX "i"
void insert(int n,int a[],int x,int i)
{int j;
 for(j=n;j>i;j--)
 {*(a+j)=*(a+j-1);}
 *(a+i)=x;
}
 
  

//PRINTS THE LIST
void print_array(int n,int a[])
{int i;
 printf("The array is");  
 printf("|");
 for(i=0;i<n;i++)
 {printf("%d |",a[i]);}
 printf("\n");
}

void main()
{int n,c,t=0,size,x,ind,d;
 printf("enter size of array:\n");
 scanf("%d",&n);
 int a[n],i,option;
 for(i=0;i<n;i++)
 {
  printf("enter a[%d]:\n",i);
  scanf("%d",&a[i]); 
 }
 print_array(n,a);
 while(t==0)
 {printf("choose an option:max(1),min(2),reverse(3),index(4),count(5),sort(6),pop(7),delete(8),append(9),extend(10),insert(11)\n");
  scanf(" %d",&option);
  switch(option)
 {
  case 1: max(n,a);break;
  case 2: min(n,a);break;
  case 3: reverse(n,a);break;
  case 4: index1(n,a);break;
  case 5: count(n,a);break;
  case 6: sort(n,a);break;
  case 7: printf("enter the index of element you want to delete:\n\n");
          scanf("%d",&ind);
          if(ind>=n)
          {printf("index value doesn't exist \n");}
          else
          {pop(n,a,ind);
          n=n-1;}break;
  case 8: d=delete(n,a);
          if(d==0)
          {printf("element doesn't exist\n");}
          else{n=n-1;}break;
  case 9: append(n,a);
          n=n+1;break;
  case 10: printf("enter the size of list that is being appended:\n");
           scanf("%d",&size);
           extend(n,a,size);
           n=n+size;break;
  case 11: 
           printf("enter the element & index at which you want it to append(x,i):\n");
           scanf("%d,%d",&x,&ind);
           if(ind>=n)
           {printf("index value doesn't exist \n");}
           else
           {insert(n,a,x,ind);
            n=n+1;}
           break;
  default: printf("wrong choice");
 }
 print_array(n,a);
 printf("Do you want to continue(y:0/n:1):\n");
 scanf("%d",&c);
 t=c;}
 
}
